package yuan.datastructure.list;

public class LinkList implements List {
	private Pointer head;
	private Pointer current;
	private int size;
	
	public LinkList(){
		head = new Pointer(null);
	}
	
	@Override
	public void clear() {
		size = 0;
		head.setNext(null);
		current = null;
	}

	@Override
	public boolean isEmpty() {
		return size==0;
	}

	@Override
	public int size() {
		return size;
	}

	@Override
	public Object getElementAt(int i) {
		if (i<0 || i>(size-1))
			return null;
		
		int index = 0;
		Pointer pointer = head.getNext();
		while(index<i){
			pointer = pointer.getNext();
			index++;
		}
		
		return pointer.getData();
	}

	@Override
	public int locateElement(Object e) {
		Pointer pointer= head.getNext();
		for (int i=0; pointer!=null;i++){
			if (pointer.getData().equals(e))
				return i;
			pointer = pointer.getNext();
		}
		return -1;
	}

	@Override
	public Object first() {
		current = head.getNext();
		return current!=null?current.getData():null;
	}

	@Override
	public Object prior() {
		if (current == null)
			return null;
		
		Pointer pointer= head.getNext();
		while (pointer!=null){
			if (pointer.getNext() == current){
				current = pointer;
				return pointer.getData();
			}
			pointer = pointer.getNext();
		}
		return null;
	}

	@Override
	public Object next() {
		if (current == null)
			return null;
		
		current = current.getNext();
		return current!=null?current.getData():null;
	}

	@Override
	public Object last() {
		Pointer pointer= head;
		while (pointer.getNext()!=null){
			pointer = pointer.getNext();
		}
		
		current = pointer;
		return pointer.getData();
	}

	@Override
	public void insert(Object e) {
		insert(e, size);
	}

	@Override
	public void insert(Object e, int i) {
		Pointer newPointer = new Pointer(e);
		Pointer pointer= head;
		int index = 0;
		while (index<i){
			pointer = pointer.getNext();
			index++;
		}
		
		newPointer.setNext(pointer.getNext());
		pointer.setNext(newPointer);
		
		if (i == 0)
			head.setNext(newPointer);
		
		current = head.getNext();
		size++;
	}

	@Override
	public void delete(Object e) {
		int index = locateElement(e);
		if (index == -1)
			throw new IllegalArgumentException("can't find object in list:"+e);
		
		delete(index);
	}

	@Override
	public void delete(int i) {
		if (i<0 || i>(size-1))
			throw new IllegalArgumentException("input index is not valid i:"+i+" size:"+size);
		
		Pointer pointer= head;
		int index = 0;
		while (index<i){
			pointer = pointer.getNext();
			index++;
		}
		
		pointer.setNext(pointer.getNext().getNext());
		
		current = head.getNext();
		size--;
	}
	
	public void reverse(){
		Pointer pre = null;
		Pointer thisOne = head.getNext();
		Pointer next = thisOne.getNext();
		
		while(next!=null){
			Pointer tempNext = next.getNext();
			
			//reverse
			thisOne.setNext(pre);
			next.setNext(thisOne);
			
			//move
			pre = thisOne;
			thisOne = next;
			next = tempNext;
			System.out.println("aaa");
		}
		
		thisOne.setNext(pre);
	}
}
